package com.abcdefg.demo.direct;

import com.abcdefg.demo.constants.Constants;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class DirectConsumer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Constants.Host);
        factory.setPort(Constants.Port);
        factory.setVirtualHost(Constants.VirtualHost);
        factory.setUsername(Constants.UserName);
        factory.setPassword(Constants.Password);
        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        //3.声明队列
        channel.queueDeclare(Constants.DIRECT_QUEUE2, true, false, false, null);

        //4.消费消息
        DefaultConsumer consumer=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收到消息: " + new String(body));
            }
        };
        channel.basicConsume(Constants.DIRECT_QUEUE2, true, consumer);
    }
}
